﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public partial class BaseData_TYLX_SX : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private string Code
    {
        set { ViewState["Code"] = value; }
        get 
        {
            if (ViewState["Code"] != null)
            {
                return ViewState["Code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }
    private string tablename = "SHUXING A left join TYLX_SHUXING B on A.SX_ID=B.SXID and B.LXID=";
    private string fieldname = "SX_ID, SX_MC, SX_TABLE, ISDEL, REMARK,IsHave=case when B.ID is null then 0 else 1 end ";
    private string where = " and A.IsDel=0";
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check.CheckPermission("200300104");
        if (Request.QueryString["code"] == null)
        {
            Response.Redirect("TYLX.aspx");
        }
        Code = Request.QueryString["code"].ToString();
        if (!IsPostBack)
        {
        
            Model.TYLX shuxing = new BLL.TYLX().GetModel(int.Parse(Code));
            Label1.Text = shuxing.TYPENAME;            
            AspNetPager1.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString());
            SearchControl1.InitParam(tablename+Code, fieldname, "SX_ID", "SX_MC", "TYLX_SXControl1", "rptSHUXING", "AspNetPager1");
            Bind();
        }
    }
    private void Bind()
    {
        int pageCount, rowCount;
        DataTable properties = new BLL.SHUXING().GetList(tablename+Code, fieldname, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "SX_ID", false, where, out pageCount, out rowCount);
        TYLX_SXControl1.Bind(properties);
        AspNetPager1.RecordCount = rowCount;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        int pageCount, rowCount;
        if (SearchControl1.IsSearchResult)
        {
            Repeater rpt = (Repeater)TYLX_SXControl1.FindControl("rptSHUXING");
            if (rpt != null)
            {
                string strSql;
                SearchControl1.BindParentPageControl(rpt, AspNetPager1, SearchControl1.DataTableName, SearchControl1.PrimaryKeyColumnName, SearchControl1.StrCondition, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out pageCount, out rowCount, out strSql);
            }
        }
        else
        {
            DataTable colors = new BLL.SHUXING().GetList(tablename+Code, fieldname, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "SX_ID", false, where, out pageCount, out rowCount);
            TYLX_SXControl1.Bind(colors);
        }
    }
    //增加关系
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<int> s = TYLX_SXControl1.Selected;
        if (s.Count < 1)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请选择属性!');", true);
            return;
        }
        else
        {
            List<Model.TYLX_SHUXING> l = new List<Model.TYLX_SHUXING>();            
            for (int i = 0; i < s.Count; i++)
            {
                Model.TYLX_SHUXING tylx_sx = new Model.TYLX_SHUXING();
                tylx_sx.LXID = int.Parse(Code);
                tylx_sx.SXID = s[i];
                l.Add(tylx_sx);
            }
            BLL.TYLX_SHUXING ts = new BLL.TYLX_SHUXING();
            ts.Add(l);

            Bind();

        }
    }
    //删除关系
    protected void Button2_Click(object sender, EventArgs e)
    {
        List<int> s = TYLX_SXControl1.Selected;
        if (s.Count < 1)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请选择属性!');", true);
            return;
        }
        else
        {
            List<Model.TYLX_SHUXING> l = new List<Model.TYLX_SHUXING>();            
            for (int i = 0; i < s.Count; i++)
            {
                Model.TYLX_SHUXING tylx_sx = new Model.TYLX_SHUXING();
                tylx_sx.LXID = int.Parse(Code);
                tylx_sx.SXID = s[i];
                l.Add(tylx_sx);
            }
            BLL.TYLX_SHUXING ts = new BLL.TYLX_SHUXING();
            ts.Delete(l);

            Bind();

        }
    }
}
